package com.sx.pipeline.ui.mine

import android.app.Application
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.Logs
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.repos.http.res.IdentityVO
import com.sx.pipeline.repos.http.res.MyUnitListVO
import com.sx.pipeline.ui.LaunchActivity
import com.sx.pipeline.utils.CacheDataManager
import io.rong.imkit.RongIM

/**
 * @Author: YY
 * @Date: 2022/6/25
 * @Desc: TODO
 */
class SwitchBusinessVM(app: Application): CYBaseViewModel(app) {

    val switchUnitListEvent: SingleLiveEvent<List<MyUnitListVO>> = SingleLiveEvent()

    fun getSwitchUnitList() {
        addSubscribe(mModel.getSwitchUnitList()
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<List<MyUnitListVO>, BaseViewModel<*>>
                (this, true) {
                override fun onResult(result: List<MyUnitListVO>) {
                    Logs.d("getSwitchUnitList ${result.size}")
                    switchUnitListEvent.postValue(result)
                }
            })
        )
    }


    fun switchUnitIdentity(identity: String, unitId: Int) {
        addSubscribe(mModel.switchUnitIdentity(identity,unitId)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<IdentityVO, BaseViewModel<*>>
                (this, true) {
                override fun onResult(result: IdentityVO) {
                    mModel.saveIdentityType(result.identity.toInt())
                    RongIM.getInstance().logout();//融云断开连接
                    CacheDataManager.clearAllCache(getApplication<Application>().applicationContext)
                    ActivityUtils.finishAllActivities()
                    startActivity(LaunchActivity::class.java)
                }
            })
        )
    }
}